<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>Madhouse|monitor core</title>
<link rel="stylesheet" href="../assets/css/bootstrap-spacelab.css" />
<link rel="stylesheet" href="../assets/css/base.css?t=20130516173114" />
<link rel="stylesheet" href="../assets/css/validationEngine.jquery.css" type="text/css"/>
  <!-- template
    ================================================== -->
    <script id="t_a" type="text/html">
    	<% var json = json || {};%>
				<form class="form-horizontal" id="form" onsubmit="return false;">
					 <fieldset>
					 	<br />
                        <div class="control-group">
                            <label class="control-label" for="name"> 用户名： </label>
                            <div class="controls">
                                <input type="text" class="validate[required,custom[onlyLetterNumber]] text-input"  name="name" value="<%= json.name || '' %>" <%= json.name && 'readonly' %> >
                                <p class="help-block">格式：数字，字母，下划线或者中划线（用户名不能含有中文）</p>
                            </div>
                        </div>
						
                        <div class="control-group">
                            <label class="control-label" for="realname"> 真实名字： </label>
                            <div class="controls">
                                <input type="text"  name="realname" value="<%= json.realname || '' %>" >
                                <p class="help-block">用户的真实名字</p>
                            </div>
                        </div>
						
                        <div class="control-group">
                            <label class="control-label" for="email"> Email： </label>
                            <div class="controls">
                                <input type="text" class="validate[required,custom[email]] text-input"  name="email" id="email" value="<%= json.email || '' %>" >
                                <p class="help-block">请输入正确的邮箱格式</p>
                            </div>
                        </div>	
						
                        <div class="control-group">
                            <label class="control-label" for="passwd"> 登录密码： </label>
                            <div class="controls">
                                <input type="password" class="validate[required] text-input" name="passwd" id="passwd" value="<%= json.passwd || '' %>" >
                                <p class="help-block">登录管理界面的密码</p>
                            </div>
                        </div>	
						
                        <div class="control-group">
                            <label class="control-label" for="mailtype"> 接受报警类型：</label>
                            <div class="controls">
                                <select name="mailtype" id="mailtype">
                                	<option value="1" <%= json.mailtype == 1 && 'selected' %> >不接收</option>
                    				<option value="2" <%= json.mailtype == 2 && 'selected' %> >普通报警</option>
                    				<option value="3" <%= json.mailtype == 3 && 'selected' %> >严重报警</option>
                    				<option value="4" <%= json.mailtype == 4 && 'selected' %> >所有报警</option>
									<option value="5" <%= json.mailtype == 5 && 'selected' %> >按用户组所属服务器组设置</option>
									<option value="6" <%= json.mailtype == 6 && 'selected' %> >按用户组所属服务器组设置(包含基础设施报警)</option>
                                </select>
								<p class="help-block">接收报警邮件的类型</p>
                            </div>
                        </div>																								
						
                        <div class="control-group">
                            <label class="control-label" for="desc"> 描述：</label>
                            <div class="controls">
                                <textarea name="desc" id="desc" rows="3"  value="<%= json.desc || '' %>" ><%= json.desc || '' %></textarea>
								<p class="help-block">可选的描述</p>
                            </div>
                        </div>					
																													
                        <div class="form-actions">
                            <button class='btn btn-info' name="submit"> 保存 </button>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<button class='btn' name="return"> 返回 </button>
                            <input type="reset" style="display:none;">
                        </div>
				</fieldset>
			</form>
			
    </script>

	
<style>

</style>

</head>
<body>
    <header>
        <h1><span id="title">修改用户&nbsp;&nbsp;&gt;&gt;&nbsp;&nbsp;</span> <span id="userName"></span></h1>
        <div class="row" id="toolbar">
                <div class="box_1">
                    <button class="btn" id="info">
                        <i class="icon-info-sign"></i> 信息
                    </button>					
                </div>
				
                <div class="btn-group" style="display:none;">
                    <button class="btn">
                        	操作
                    </button>
                    <button class="btn dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#" id="notification"></a></li>
                    </ul>
                </div>
        </div>
    </header>	
	
    <div id="mainbody">
        <div class="tabbable">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#user_form" data-toggle="tab">基本信息</a>
                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="user_form">
                </div>
            </div>
        </div>    	
    </div>
	

  <!-- 弹出对话框
    ================================================== -->
<div class="modal fade" id="modal">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>OK</h3>
  </div>
  <div class="modal-body">
  	
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" name="return">返回</a>
    <a href="#" class="btn btn-info" id="nextEdit">继续修改</a>
  </div>
</div>


  <!-- js part
    ================================================== -->
<script src="../assets/js/library/jquery-1.7.1.js"></script>
<script src="../assets/js/library/bootstrap.min.js"></script>
<script src="../assets/js/library/template.js"></script>
<script src="../assets/js/library/jquery.validationEngine-en.js"></script>
<script src="../assets/js/library/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
<script src="../assets/js/helper.js?t=20130516173114"></script>
<script src="../assets/js/doc.js?t=20130516173114"></script>	
<script>
$(function(){
	
	var smartMad = window.smartMad = window.smartMad || {};
	
	var userName = urlParams('name') || '';
	
	////////////////////////////////////////////////////
	//常规选项设置
	////////////////////////////////////////////////////
	var userFormConf = {
		url: smartMad._url + smartMad.api.getUser + userName
	};	
	var userFormCall = function(e,json){
		var $th = $(this), data, html;
		
		//$th.html(JSON.stringify(json));return;
        ///////////////////////////////////////////////
		
		json.name = userName;
		
		smartMad.json = json;
		
		///////////////////////////////////////////////
        data = {
            json: json
        };
        
        ///////////////////////////////////////////////
        
        html = template('t_a', data);
        $th.html(html);
		
		$('#form').validationEngine();
		
	};	
	
	
	////////////////////////////////////////////////////////
	//提交保存
	////////////////////////////////////////////////////////
    $('#mainbody').delegate('button[name="submit"]', 'click', function(){
		//表单验证
		if(!$('#form').validationEngine('validate')){
			return;
		}
		
        var conf = {
        };
		
        var name = $('#user_form input[name="name"]').val(),
			realname = $("#user_form input[name='realname']").val(),
			email = $("#user_form input[name='email']").val(),
			passwd = $("#user_form input[name='passwd']").val(), 
        	mailtype = $("#user_form select[name='mailtype']").find("option:selected").val(),
			desc = $("#user_form textarea[name='desc']").val();
        
        conf.data = {
            "realname": realname,
            "email": email,
            "passwd": passwd,
            "mailtype": mailtype,
            "desc": desc
        };	
		
		//判断是修改还是创建
        
        if (userName) {
            conf.url = smartMad._url + smartMad.api.updateUser + userName;
        }
        else {
            conf.success = function(){
                $('#modal').modal('show').find('.modal-body').html('添加成功!');
            };
            conf.url = smartMad._url + smartMad.api.createUser + name;
        }
		
		smartMad.userName = $('#user_form input[name="name"]').val();
		
        $(this).postData(conf);
    });
	
	/////////////////////////////////////////////////////
	
	$('body')
	.delegate('[name="return"]', 'click', function(){
	    location.href = 'userManager.html?version=' + smartMad.version;
	 })
	 .delegate('#nextEdit','click',function(){
	 	location.href = 'editUser.html?name=' + smartMad.userName + '&version=' + smartMad.version;
	 });
	 
    
	////////////////////////////////////////////////////////
	//init
	////////////////////////////////////////////////////////
	
    if (userName) {
        $('#userName').text(userName);
        $('#user_form').bind('ajax.success', userFormCall).getData(userFormConf);
    }else{
		 $('#title').text('添加新用户');
		 $('#user_form').bind('ajax.success', userFormCall).trigger('ajax.success',[{}]);
	}

});
</script>
</body>
</html>
